/**
 * Created by python on 19-3-1.
 */
var vm = new Vue({
    el: '#app',
    data: {
        username: sessionStorage.username || localStorage.username,
        user_id: sessionStorage.user_id || localStorage.user_id,
        token: sessionStorage.token || localStorage.token,
        avatar_url:sessionStorage.avatar_url || localStorage.avatar_url,
        question_id: '',
        author_id: '',
        author_name: '',
        create_time: '',
        update_time: '',
        label_name: '',
        label_id: '',
        clicks: '',
        title: '',
        question_content: '',
        answersList:[],
        answers_count:'',
        answers_content:'',
        similarityList:[],
        server_time:''

    },
    mounted: function () {
        this.question_id = get_query_string('question_id');
        axios.get(HOST + '/questions/' + this.question_id + '/', {
            responseType: 'json'
        })
            .then(response => {

                this.question_id = response.data.id;
                this.author_id = response.data.author_name.id;
                this.author_name = response.data.author_name.username;
                this.create_time = response.data.create_time;
                this.update_time = response.data.update_time;
                this.label_id = response.data.labels[0].id;
                this.label_name = response.data.labels[0].name;
                this.clicks = response.data.clicks;
                this.title = response.data.title;
                this.question_content = response.data.content
            })
            .catch(error => {
                console.log(error.response.data)
            });
        axios.get(HOST + '/questions/' + this.question_id + '/answers/', {
            responseType: 'json'
        })
            .then(response => {
                this.answersList = response.data
                this.answers_count = response.data.length
                this.server_time = response.headers.date;
            })
            .catch(error => {
                console.log(error.response.data)
            });
        axios.get(HOST + '/questions/' + this.question_id + '/similarity/', {
            responseType: 'json'
        })
            .then(response => {
                console.log('像是问题')
                console.log(response.data)
                this.similarityList = response.data

            })
            .catch(error => {
                console.log(error.response.data)
            });
    },
    methods:{
        on_content:function (parent_id) {
            axios.post(HOST + '/questions/' + this.question_id + '/answers/', {
                    parent: parent_id,
                    content: this.answers_content,
                }, {
                    headers: {
                        'Authorization': 'JWT ' + this.token
                    },
                    responseType: 'json',

                })
                .then(response => {
                    console.log(response.data)
                    alert('发布评论成功');
                    location.reload();


                })
                .catch(error => {
                    alert('发布失败')
                })
        },
        on_like:function (click_id,action,click_type) {
            axios.post(HOST + '/questionlikes/', {
                    action : action,
                    click_type : click_type,
                    click_id : click_id

                }, {
                    headers: {
                        'Authorization': 'JWT ' + this.token
                    },
                    responseType: 'json',

                })
                .then(response => {
                    console.log(response.data)
                    if(action=='like'){
                        if (click_type=='question'){
                            this.clicks += 1
                        }else {
                            for(var i=0;i<this.answers_count;i++){
                                if (this.answersList[i].id == click_id){
                                    this.answersList[i].clicks += 1
                                    return
                                }
                            }
                        }

                    }else {
                        if (click_type=='question'){
                            this.clicks -= 1
                        }else {
                            for(var i=0;i<this.answers_count;i++){
                                if (this.answersList[i].id == click_id){
                                    this.answersList[i].clicks -= 1
                                    return
                                }
                            }
                        }

                    }


                })
                .catch(error => {
                    console.log(error.response)
                    if (error.response.data.msg){
                        alert(error.response.data.msg)
                    }else {
                        alert('发布失败')
                    }

                })
        }
        
        
    }


})
